var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myOrderTopConHeight: '',   //我的订单高度
    startFlag: 1,              //分页标识   
    state: '0',
    showModelDel: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync('token');             //用户的token信息

    that.setData({
      token: token
    })
    that.getHeight();

    var state = options.state;

    that.setData({
      state: state
    });

    if (state == 0) {
      that.initMyorders(token, 0, 10);
    } else {
      that.getOrdersByState(token, state, 0, 10);
    }

  },

  /**
   * 获取 高度
   */
  getHeight: function () {
    var that = this;

    var query = wx.createSelectorQuery();
    var myOrderTopConHeight = "";
    //获取头部高度
    query.select('#myOrderTopCon').boundingClientRect()
    query.exec(function (res) {
      myOrderTopConHeight = res[0].height;

      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            myOrderTopConHeight: res.windowHeight - myOrderTopConHeight - 20
          });
        }
      });
    });
  },

  /**
   * 初始化我的订单
   */
  initMyorders: function (token, start, length) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/myorders", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        start: start,
        length: length
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          var orderList = res.data.orderList
          that.ordersPaging(start, orderList)
        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
  * 下拉
  */
  scrollLower: function () {
    var that = this;

    var start = that.data.startFlag;
    var token = that.data.token;

    var state = that.data.state;

    if (state == 0) {
      that.initMyorders(token, start, 10);
    } else {
      that.getOrdersByState(token, state, start, 10);
    }

  },

  /**
   * 商品分页
   */
  ordersPaging: function (start, orderData) {
    var that = this;

    if (start > 1) {
      var ordersLists = that.data.ordersList;
      // 追加数据
      for (var i = 0; i < orderData.length; i++) {
        ordersLists.push(orderData[i])
      }
      start = parseInt(start) + 1;
    } else {
      var ordersLists = orderData;
      start = 2;
    }
    console.log(ordersLists)
    that.setData({
      ordersList: ordersLists,
      startFlag: start
    })
  },

  /**
   * 我的订单筛选
   */
  orderStateClick: function (e) {
    var that = this;
    var token = that.data.token;
    var state = e.currentTarget.dataset.state;

    that.setData({
      state: state
    });

    if (state == 0) {
      that.initMyorders(token, 0, 10);
    } else {
      that.getOrdersByState(token, state, 0, 10);
    }

  },

  /**
   * 根据订单状态获取订单
   */
  getOrdersByState: function (token, state, start, length) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/getOrdersByState", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        state: state,
        start: start,
        length: length
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var orderList = res.data.orderList
          that.ordersPaging(start, orderList)
        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 开启是否取消
   */
  showModel: function (e) {
    var that = this;

    var orderNo = e.currentTarget.dataset.orderno;
    that.setData({
      orderNo: orderNo
    })

    that.setData({
      showModel: true
    })
  },


  /**
   * 删除订单
   */
  showModelDel: function (e) {
    var that = this;

    var orderNo = e.currentTarget.dataset.orderno;
    that.setData({
      orderNo: orderNo
    })

    that.setData({
      showModelDel: true
    })
  },

  /**
   * 取消确认按钮
   */
  confirmQx: function () {
    var that = this;
    var orderNo = that.data.orderNo;
    var token = that.data.token;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/cancelorders", //仅为示例，并非真实的接口地址
      data: {
        orderNo: orderNo,
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.showMsg(msg, 'success');
          //刷新列表
          var state = that.data.state;
          if (state == 0) {
            that.initMyorders(token, 0, 10);
          } else {
            that.getOrdersByState(token, state, 0, 10);
          }

        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 删除订单按钮
   */
  confirmDel: function () {
    var that = this;
    var orderNo = that.data.orderNo;
    var token = that.data.token;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/delorders", //仅为示例，并非真实的接口地址
      data: {
        orderNo: orderNo,
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.showMsg(msg, 'success');
          //刷新列表
          var state = that.data.state;
          if (state == 0) {
            that.initMyorders(token, 0, 10);
          } else {
            that.getOrdersByState(token, state, 0, 10);
          }

        } else {
          that.showMsg(msg, 'none');
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 关闭选择规格
   */
  closeModel: function () {
    var that = this;
    that.setData({
      showModel: false,
      showModelDel: false,
    })
  },

  /**
   * 跳转到订单详情
   */
  goOrderDetail: function (e) {
    var orderNo = e.currentTarget.dataset.orderno;
    wx.navigateTo({
      url: "../orderDetail/orderDetail?orderNo=" + orderNo,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /***
   * 搜索订单
  */
  searchInput: function (e) {
    var value = e.detail.value;
    this.goPage('../../goods/searchorderNo/searchorderNo?value=' + value)
  },
  /***
   * 查看物流
  */
  logistics: function () {
    this.goPage('../../order/logistics/logistics')
  },
  /**
* 跳转页面
*/
  goPage: function (url) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();
      }
    })
  },
  /**
   * 提示信息
   */
  showMsg: function (msg, icon) {
    wx.showToast({
      title: msg,
      icon: icon,
      duration: 2000,
      mask: true
    });
  },
})